/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.unifor.redes1.odirabel.gui.server;

import br.unifor.redes1.odirabel.ConfigProperties;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author pedro
 */
public class ThreadMainServer extends Thread {

    private JTextArea textArea;
    private ServerSocket serverSocket;
    private ClientesBuffer buffer;
    private JScrollPane scrollPane;

    ThreadMainServer(JTextArea jTextArea1, JScrollPane jScrollPane) throws IOException {
        this.textArea = jTextArea1;
        this.scrollPane = jScrollPane;
        textArea.append("Startando Server\n");
        jScrollPane.getVerticalScrollBar().setValue(jScrollPane.getVerticalScrollBar().getMaximum());
        buffer = new ClientesBuffer();
        try {
            serverSocket = new ServerSocket(Integer.parseInt(ConfigProperties.getInstance().getProperty("portaservidor")));
        } catch (IOException ex) {
            throw ex;
        }
    }

    public void run() {
        while (Boolean.TRUE) {
            try {
                new ThreadAcceptClient(serverSocket.accept(), textArea, buffer, scrollPane).start();
            } catch (UnknownHostException ex) {
                Logger.getLogger(ThreadMainServer.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(ThreadMainServer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
